import Vue from "vue";
import store from "@/store";
import VueRouter from "vue-router";
import Login from "@/views/login";
import Layout from "@/views/layout";
import Dashboard from "@/views/dashboard";

Vue.use(VueRouter);

const routes = [
  { path: "/login", component: Login },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: Dashboard,
      },
      {
        path: "/branches/organization-manage",
        component: () => import("@/views/branches/organization-manage"),
      },
      {
        path: "/branches/institutions-jobs-area",
        component: () => import("@/views/branches/institutions-jobs-area"),
      },
      {
        path: "/transit/freight-manage",
        component: () => import("@/views/branches/freight-manage"),
      },
      {
        path: "transit/car-models",
        component: () => import("@/views/transit/car-models.vue"),
      },
      {
        path: "transit/vehicle",
        component: () => import("@/views/transit/vehicle.vue"),
      },
      {
        path: "transit/car-register",
        component: () => import("@/views/transit/car-register.vue"),
      },
      {
        path: "branches/operational-range",
        component: () => import("@/views/staff/operational-range.vue"),
      },
      {
        path: "transit/driver",
        component: () => import("@/views/staff/driver.vue"),
      },
      {
        path: "transit/workArrange-manage",
        component: () => import("@/views/staff/workArrange-manage.vue"),
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});
// 前置导航守卫
router.beforeEach((to, from, next) => {
  const { token } = store.state.user;
  if (to.path !== "/login" && !token) return next("/login");
  next();
});

export default router;
